﻿<!DOCTYPE html>

<html>
<head>
<!--
  
  Copyright (c) 2016-2017 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
  
-->	
  <title>ol-ext: Color filter on map</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <meta name="description" content="Color filter on an ol map or layer." />
  <meta name="keywords" content="ol, openlayers, filter, color, contrast, lunilosity, hue, saturation" />

  <link rel="stylesheet" href="../style.css" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
  <!-- FontAwesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>

  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

  <style>
    input[type="range"] {
      vertical-align: middle;
      display: block;
      width: 20em;
      margin: 0;
    }
    input[type="number"] {
      width:4em;
    }
  </style>

</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Color filter on map</h1>
  </a>
  <div class="info">
    <i>ol.filter.Colorize</i> add colors effects on a map (hue, saturation, grayscale, invert, enhance...). 
    It uses canvas composite operations to achieve the effects.
    <br />
    You can customize effetcs, colors and intensity (value) of the filters.
    <br />
    Predefined filters are avaliable as shortcut (grayscale, sepia, invert).
  </div>

  <!-- Map div -->
  <div id="map" style="width:600px; height:400px;"></div>

  <div class="options" >
    <ul><li>
      Filter:
      <select id="filter" onchange="setFilter()">
        <optgroup label="Operation">
          <option value="color">color</option>
          <option value="difference">difference</option>
          <option value="color-dodge">color-dodge</option>
          <option value="hue">hue</option>
          <option value="saturation">saturation</option>
          <option value="luminosity">luminosity</option>
          <option value="enhance">enhance</option>
          <option value="contrast">contrast</option>
        </optgroup>
        <optgroup label="Predefined">
          <option value="grayscale">grayscale</option>
          <option value="invert">invert</option>
          <option value="sepia">sepia</option>
        </optgroup>
      </select>
    </li><li>
      <input id="active" type="checkbox" checked="checked" /><label for="active"> active</label>
    </li><li>
      <input id="enhance" type="checkbox" /><label for="enhance"> enhance before filter</label>
    </li><li>
      R:<input id="red" type="number" min="0" max="255" value="153" />
      G:<input id="green" type="number" min="0" max="255" value="102" />
      B:<input id="blue" type="number" min="0" max="255" value="51" />
    </li><li>
      Value: <input id="val" list="marks" type="range" min="0" max="1" step="0.1" value="1" />
    </li></ul>
  </div>

  <datalist id="marks">
    <option value="0" label="0">
    <option value=".1">
    <option value=".2">
    <option value=".3">
    <option value=".4">
    <option value=".5" label=".5">
    <option value=".6">
    <option value=".7">
    <option value=".8">
    <option value=".9">
    <option value="1" label="1">
  </datalist>

  <script type="text/javascript">
    $("input").on('change', setFilter);

    var layers = [ new ol.layer.Tile({ source: new ol.source.OSM() }) ];
    var layer = layers[0];

    // The map
    var map = new ol.Map
      ({	target: 'map',
        view: new ol.View
        ({	zoom: 8,
          center: [247044, 6549736.]
        }),
        layers: layers
      });

    // Enhance filter
    var enhance = new ol.filter.Colorize({ operation:'enhance', value: 1 });
    layer.addFilter(enhance);

    // Custom filter
    var filter = new ol.filter.Colorize();
    layer.addFilter(filter);

    function setFilter()
    {	enhance.setActive($("#enhance").prop('checked'));
      filter.setActive($("#active").prop('checked'));
      var f = $("#filter").val();
      switch (f)
      {	case 'grayscale':
        case 'invert':
        case 'sepia':
          filter.setFilter(f);
          break;
        default:
          filter.setFilter({ operation:f, red:Number($("#red").val()),
            green: Number($("#green").val()), blue: Number($("#blue").val()), 
            value: Number($("#val").val()),
            });
          break;

      }
    }
    setFilter();

    map.addControl(new ol.control.Permalink({ visible: false }));

  </script>

</body>
</html>